package com.just.recruitback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.just.recruitback.entity.Resume;
import com.just.recruitback.entity.Skill;
import com.just.recruitback.service.ResumeService;
import com.just.recruitback.service.base.IResumeService;
import com.just.recruitback.service.base.ISkillService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * @author : baibaiwucahng
 * @date : 2020/7/5  1:32
 */
@Service
public class ResumeServiceImpl implements ResumeService {
    @Resource
    private ISkillService iSkillService;
    @Resource
    private IResumeService iResumeService;

    @Override
    public Resume getOne(int userId) {
        QueryWrapper<Resume> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        Resume resume = iResumeService.getOne(queryWrapper);
        if (ObjectUtils.isNotEmpty(resume)) {
            QueryWrapper<Skill> skillQueryWrapper = new QueryWrapper<>();
            skillQueryWrapper.eq("resume_id", resume.getId());
            resume.setSkills(iSkillService.list(skillQueryWrapper));
        }
        return resume;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Resume resume) {
        QueryWrapper<Resume> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", resume.getUserId());
        iResumeService.remove(queryWrapper);
        iResumeService.save(resume);
        iSkillService.saveBatch(resume.getSkills().stream().peek(skill -> skill.setResumeId(resume.getId())).collect(Collectors.toList()));
    }
}
